package com.cuiweiyou.vigilesmicrecord.ui;

import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.cuiweiyou.vigilesmicrecord.R;
import com.cuiweiyou.vigilesmicrecord.base.ExitAppActivity;
import com.cuiweiyou.vigilesmicrecord.util.PlayUtil;
import com.cuiweiyou.vigilesmicrecord.util.RecordUtil;
import com.cuiweiyou.vigilesmicrecord.view.TimerTextView;
import com.cuiweiyou.vigilesmicrecord.view.VolumeWaveView;

import java.io.File;

import butterknife.BindView;
import butterknife.OnClick;

public class MainActivity extends ExitAppActivity {

    @BindView(R.id.timerTextView)
    TimerTextView timerTextView;

    @BindView(R.id.lastFileTextView)
    TextView lastFileTextView;

    @BindView(R.id.volumeWaveView)
    VolumeWaveView volumeWaveView;

    @BindView(R.id.playImageView)
    ImageView playImageView;

    @BindView(R.id.recordImageView)
    ImageView recordImageView;

    @BindView(R.id.soundListImageView)
    ImageView soundListImageView;

    String lastRecordFile;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        PlayUtil.getInstance().release();
        RecordUtil.getInstance().release();
    }

    @Override
    public void initView() {
        RecordUtil.getInstance().init();
        RecordUtil.getInstance().setOnRecordStopListener(onRecordStopListener);
        RecordUtil.getInstance().setSoundHandler(volumeWaveView.getWaveHandler());
        PlayUtil.getInstance().setVolumeHandler(volumeWaveView.getWaveHandler());
    }

    @OnClick(R.id.soundListImageView)
    public void onSettingImageViewClicked() {
        nextActivityKeepCurrent(SoundsActivity.class);
    }

    @OnClick(R.id.playImageView)
    public void onPlayImageViewClicked() {
        boolean isRecording = RecordUtil.getInstance().isRecording();
        if (isRecording) {
            return;
        }

        if (null == lastRecordFile) {
            Toast.makeText(MainActivity.this, "无效的录音文件", Toast.LENGTH_SHORT).show();
            return;
        }

        File f = new File(lastRecordFile);
        if (!f.exists()) {
            Toast.makeText(MainActivity.this, "没有找到录音文件", Toast.LENGTH_SHORT).show();
            return;
        }

        PlayUtil.getInstance().startPlay(lastRecordFile);
    }

    @OnClick(R.id.recordImageView)
    public void onRecordViewClicked() {
        boolean isPlaying = PlayUtil.getInstance().isPlaying();
        if (isPlaying) {
            PlayUtil.getInstance().stopPlay();
        }

        boolean isRecording = RecordUtil.getInstance().isRecording();
        if (isRecording) {
            RecordUtil.getInstance().stopRecord();
        } else {
            lastRecordFile = null;
            timerTextView.startTimer();
            playImageView.setEnabled(false);
            soundListImageView.setEnabled(false);
            playImageView.setImageResource(R.mipmap.play_n);
            recordImageView.setImageResource(R.mipmap.recording);
            RecordUtil.getInstance().startRecord();
        }
    }

    RecordUtil.OnRecordStopListener onRecordStopListener = new RecordUtil.OnRecordStopListener() {
        @Override
        public void recordStop(String wavFilePath) {
            recordImageView.setImageResource(R.mipmap.record);
            playImageView.setImageResource(R.mipmap.play);
            soundListImageView.setEnabled(true);
            playImageView.setEnabled(true);
            timerTextView.stopTimer();

            lastRecordFile = wavFilePath;
            lastFileTextView.setText(new File(lastRecordFile).getName());
        }
    };
}


